package com.mystore.console.controller;

import com.mystore.common.jsonx.JSONEntity;
import com.mystore.common.utils.string.VerifyCodeUtils;
import com.mystore.framework.mail.MailSessionBean;
import com.mystore.framework.mail.page.impl.VerifyMailPage;
import com.mystore.framework.pojo.User;
import com.mystore.framework.service.impl.IUserServiceImpl;
import com.mystore.framework.service.impl.RegisterRedisServiceImpl;
import com.mystore.framework.utils.UserInformationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("author/user")
public class ConsoleController {
    @Resource
    private IUserServiceImpl iUserServiceImpl;
    @Resource
    private MailSessionBean mailSessionBean;
    @Resource
    private RegisterRedisServiceImpl registerRedisServiceImpl;
    @GetMapping("/loginPage.do")
    public String loginPage(){
        return "console_login";
    }
    @PostMapping("/login.do")
    public String login(User user, Model model, HttpServletRequest request){
        HttpSession session=request.getSession();
        User myUser;
        if ((myUser=iUserServiceImpl.login(user))!=null){
            System.out.println(myUser);
            session.setAttribute("userId",myUser.getUserId());
            return "redirect:/index.do";
        }
        else{
            model.addAttribute("msg","账号或密码错误");
            return "console_login";
        }
    }
    @GetMapping("/register.do")
    public String register(){
        return "console_register";
    }
    @PostMapping("/toRegister.do")
    public String toRegister(User user, Model model, RedirectAttributes attributes){
        user.setEnabled(0);
        int userId=-1;
        String code=VerifyCodeUtils.buildVerifyCode();
        if(iUserServiceImpl.selectByUserEmail(user.getUserEmail())!=null) {
            model.addAttribute("msg","注册失败，此账号已注册");
            return "console_register";
        }
        User register=iUserServiceImpl.selectNotCompleteRegister(user.getUserEmail());
        if(register!=null){
            registerRedisServiceImpl.put(String.valueOf(register.getUserId()), code);
            try {
                mailSessionBean.sendMail(user.getUserEmail(),"MyStore账户验证",new VerifyMailPage().initMail(code));
                register.setUserName(user.getUserName());
                register.setUserPassword(user.getUserPassword());
                iUserServiceImpl.updateOne(register);
            } catch (MessagingException e) {
                model.addAttribute("msg","注册失败，请检查您输入的信息");
                return "console_register";
            }
            model.addAttribute("userId",register.getUserId());
            return "console_verify";
        }
        if ((userId=iUserServiceImpl.insertOne(user))>0){
            registerRedisServiceImpl.put(String.valueOf(userId), code);
            try {
                mailSessionBean.sendMail(user.getUserEmail(),"MyStore账户验证",new VerifyMailPage().initMail(code));
            } catch (MessagingException e) {
                model.addAttribute("msg","注册失败，请检查您输入的信息");
                return "console_register";
            }
            model.addAttribute("userId",userId);
            return "console_verify";
        }
        else{
            model.addAttribute("msg","注册失败，请检查您输入的信息");
            return "console_register";
        }
    }
    @PostMapping("/verify.do")
    public String verify(int userId,String verifyCode, Model model){
        User user= iUserServiceImpl.selectById(userId);
        String code= (String) registerRedisServiceImpl.get(String.valueOf(userId));
        if (user==null|code==null){
            model.addAttribute("flag","error");
            return "console_verify_msg";
        }
        else{
            if(code.equals(verifyCode)) {
                user.setEnabled(1);
                if(iUserServiceImpl.updateOne(user)){
                    model.addAttribute("flag","ok");
                    return "console_verify_msg";
                }else{
                    model.addAttribute("flag","error");
                    return "console_verify_msg";
                }
            }
            else{
                model.addAttribute("userId",user.getUserId());
                model.addAttribute("msg","验证码错误，请重新输入");
                return "console_verify";
            }
        }
    }
}
